package environment;

import machinery.Machinery;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 观察者模式：环境监测器作为被观察者
 */
public class EnvironmentMonitor {
    private List<Machinery> observers;
    private List<Sensor> sensors;
    private Map<String, Object> environmentData;
    
    public EnvironmentMonitor() {
        this.observers = new ArrayList<>();
        this.sensors = new ArrayList<>();
        this.environmentData = new HashMap<>();
        
        // 初始化环境数据
        environmentData.put("soilMoisture", 50); // 默认土壤湿度
        environmentData.put("temperature", 25);  // 默认温度


        environmentData.put("windSpeed", 3);     // 默认风速
    }
    
    // 添加观察者（农机设备）
    public void addObserver(Machinery machinery) {
        observers.add(machinery);
        System.out.println("添加观察者: " + machinery.getName());
    }
    
    // 移除观察者
    public void removeObserver(Machinery machinery) {
        observers.remove(machinery);
        System.out.println("移除观察者: " + machinery.getName());
    }
    
    // 添加传感器
    public void addSensor(Sensor sensor) {
        sensors.add(sensor);
        System.out.println("添加传感器: " + sensor.getType());
    }
    
    // 通知所有观察者
    private void notifyObservers() {
        for (Machinery observer : observers) {
            observer.update(environmentData);
        }
    }
    
    // 更新土壤湿度
    public void updateSoilMoisture(int moisture) {
        System.out.println("土壤湿度更新为: " + moisture + "%");
        environmentData.put("soilMoisture", moisture);
        notifyObservers();
    }
    
    // 更新温度
    public void updateTemperature(int temperature) {
        System.out.println("温度更新为: " + temperature + "℃");
        environmentData.put("temperature", temperature);
        notifyObservers();
    }
    
    // 更新风速
    public void updateWindSpeed(int windSpeed) {
        System.out.println("风速更新为: " + windSpeed + "m/s");
        environmentData.put("windSpeed", windSpeed);
        notifyObservers();
    }
} 